﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;

namespace ISIM
{
    public partial class ChatTab : TabControl
    {
        private ChatTabPage previouslySelectedTab;

        public ChatTab()
        {
            InitializeComponent();

            this.Resize += ChatTabs_Resize;
            this.Deselecting += ChatTab_Deselecting;

            SetStyle(ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.UserPaint, true);
        }

        void ChatTab_Deselecting(object sender, TabControlCancelEventArgs e)
        {
            if (this.SelectedTab != null)
                previouslySelectedTab = this.SelectedTab as ChatTabPage;
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            
            Graphics g = e.Graphics;

            Rectangle TabControlArea = this.ClientRectangle;
            Rectangle TabArea = this.DisplayRectangle;
            TabArea = new Rectangle(TabArea.X - 1, TabArea.Y, TabArea.Width + 1, TabArea.Height);


            //----------------------------
            // fill client area
            g.FillRectangle(new SolidBrush(Color.WhiteSmoke), TabControlArea);
            //----------------------------

            //----------------------------
            // draw border
            int nDelta = SystemInformation.Border3DSize.Width;

            TabArea.Inflate(nDelta, nDelta);
            g.DrawRectangle(new Pen(SystemColors.ControlDark), TabArea);
            //----------------------------
            
            foreach (ChatTabPage tab in this.TabPages)
                DrawTab(tab, e);
        }

        private void DrawTab(ChatTabPage tab, PaintEventArgs e)
        {
            Rectangle recBounds = this.GetTabRect(TabPages.IndexOf(tab));            
            if (tab != SelectedTab)
                recBounds = new Rectangle(recBounds.X, recBounds.Y + 3, recBounds.Width, recBounds.Height - 3);

            Point[] pt = new Point[7];
            pt[0] = new Point(recBounds.Left, recBounds.Bottom);
            pt[1] = new Point(recBounds.Left, recBounds.Top + 2);
            pt[2] = new Point(recBounds.Left + 2, recBounds.Top);
            pt[3] = new Point(recBounds.Right - 2, recBounds.Top);
            pt[4] = new Point(recBounds.Right, recBounds.Top + 2);
            pt[5] = new Point(recBounds.Right, recBounds.Bottom);
            pt[6] = new Point(recBounds.Left, recBounds.Bottom);

            Color tabColor;
            // Rámečky
            if ((SelectedTab == tab) && (((tab.contact != null) && (tab.contact.isComposingMessage != true)) || (tab.contact == null)))
                tabColor = Color.White;
            else if ((tab.contact != null) && (tab.contact.unreadedMessage == true))
                tabColor = Color.Orange;
            else if ((tab.contact != null) && (tab.contact.isComposingMessage == true))
                tabColor = Color.SkyBlue;
            else
                tabColor = Color.LightGray;

            e.Graphics.FillPolygon(new SolidBrush(tabColor), pt);
            e.Graphics.DrawPolygon(SystemPens.ControlDark, pt);

            if (SelectedTab == tab)
                e.Graphics.DrawLine(new Pen(tabColor), recBounds.Left + 1, recBounds.Bottom, recBounds.Right - 1, recBounds.Bottom);
                
            // Ikonka dostupnosti, případně psaní nepřečtené zprávy
            Bitmap image;
            if ((tab.contact != null) && (tab.contact.unreadedMessage == true))
                image = new Bitmap(Global.appRunPath + "graphics" + System.IO.Path.DirectorySeparatorChar + "messageIcon.png");
            else if ((tab.contact != null) && (tab.contact.isComposingMessage == true))
                image = new Bitmap(Global.appRunPath + "graphics" + System.IO.Path.DirectorySeparatorChar + "composingIcon.png");
            else
            {
                if (Global.settingsDictionary["bannedList"].ContainsKey(tab.contact.id))
                    image = Global.Converter.GetContactAvailabilityImage(Global.Availability.Blocked);
                else if (Global.OpenForms.mainForm.contactListView.Items.ContainsKey(tab.Name))
                    image = Global.Converter.GetContactAvailabilityImage(tab.contact.availability);
                else
                    image = new Bitmap(Global.appRunPath + "graphics" + System.IO.Path.DirectorySeparatorChar + "conferenceIcon.png");
            }

            e.Graphics.DrawImage(image, recBounds.Left + 5, recBounds.Top + 5, 12, 12);
            
            e.Graphics.DrawString(Global.Converter.GetRenamedContactName(tab.contact).SortText(16), Font, Brushes.Black, recBounds.Left +  21, recBounds.Top + 3);
        }

        public void AddTab(ISIMContact contact, bool selectTab = false)
        {
            if (!this.TabPages.ContainsKey(contact.id))
            {
                ChatTabPage ctp = new ChatTabPage(contact);
                ctp.convRtb.AppendText(Global.ISIMFunctions.LoadConversation(contact.id, 32));
                ctp.convRtb.Scroll();

                this.TabPages.Add(ctp);
                if (selectTab)
                    this.SelectedTab = ctp;

                if (contact.contactObject is XmppContact.ContactObject)
                    Global.ISIMFunctions.GetXmppContactAvatar((contact.contactObject as XmppContact.ContactObject).xmppClient, contact.id);
            }
            else if (selectTab)
                this.SelectedTab = this.TabPages[contact.id];
        }

        public void RemoveTab(TabPage tab)
        {
            if ((previouslySelectedTab != null) && (this.TabPages.Contains(previouslySelectedTab)))
                this.SelectedTab = previouslySelectedTab;
            this.TabPages.Remove(tab);
        }

        public void ChatTabs_Resize(object sender, EventArgs e)
        {
            if ((this.TabCount == 0) || (this.SelectedTab == null))
                return;

            ConversationRTB convRtb = (this.SelectedTab as ChatTabPage).convRtb;
            TextBox msgTb = (this.SelectedTab as ChatTabPage).msgTb;
            Button sendBtn = (this.SelectedTab as ChatTabPage).sendBtn;

            convRtb.Size = new Size(this.Width - 6, this.Height - msgTb.Height - 31);

            msgTb.Width = this.Width - sendBtn.Width - 8;
            msgTb.Top = convRtb.Height + 2;

            sendBtn.Top = msgTb.Top - 1;
            sendBtn.Left = this.Width - sendBtn.Width - 7;

            convRtb.Scroll();
        }
    }
}
